package universal

import (
	stdContext "context"
	"gitee.com/xlizy/common-go/base/common_models"
	"gitee.com/xlizy/common-go/base/common_rpc"
	"gitee.com/xlizy/common-go/base/enums/common_error"
	"gitee.com/xlizy/common-go/base/enums/dic_value_type"
	"gitee.com/xlizy/common-go/base/response"
	"gitee.com/xlizy/common-go/utils/json"
	"gitee.com/xlizy/common-go/utils/zlog"
	"gitee.com/xlizy/rpc-interface/rpc_pbs"
	"github.com/shopspring/decimal"
	"strconv"
	"strings"
)

// GetDicItemValue 获取字典项值
func GetDicItemValue(groupCode, dicKey string) *response.Response {
	req := &rpc_pbs.DicItemReq{
		GroupCode: groupCode,
		DicKey:    dicKey,
	}
	rsp := common_rpc.R(common_rpc.UniversalServiceClientImpl().GetDicItemValue(stdContext.TODO(), req))
	if rsp.Success {
		dicItemInfo := &rpc_pbs.DicItemResp{}
		rsp.RpcData(dicItemInfo)
		valueType := dic_value_type.DicValueType(dicItemInfo.DicValueType)
		if valueType == dic_value_type.String {
			return response.Success("获取成功", dicItemInfo.DicValue)
		} else if valueType == dic_value_type.Integer {
			value, e := strconv.Atoi(dicItemInfo.DicValue)
			if e != nil {
				return response.Error(common_error.DicItemNotExist, nil)
			} else {
				return response.Success("获取成功", value)
			}
		} else if valueType == dic_value_type.Decimal {
			value, e := decimal.NewFromString(dicItemInfo.DicValue)
			if e != nil {
				return response.Error(common_error.DicItemNotExist, nil)
			} else {
				return response.Success("获取成功", value.InexactFloat64())
			}
		} else if valueType == dic_value_type.Bool {
			if strings.ToLower(dicItemInfo.DicValue) == "true" {
				return response.Success("获取成功", true)
			} else {
				return response.Success("获取成功", false)
			}
		} else if valueType == dic_value_type.JsonObject {
			value := make(map[string]any)
			json.ToObj(dicItemInfo.DicValue, &value)
			return response.Success("获取成功", value)
		} else if valueType == dic_value_type.JsonArray {
			value := make([]any, 0)
			json.ToObj(dicItemInfo.DicValue, &value)
			return response.Success("获取成功", value)
		} else {
			return response.Error(common_error.DicItemNotExist, nil)
		}
	} else {
		zlog.Info("获取字典项失败,msg:{}", rsp.Msg)
		return rsp
	}
}

// GetDicItemObj 获取字典项完整信息
func GetDicItemObj(groupCode, dicKey string) *response.Response {
	req := &rpc_pbs.DicItemReq{
		GroupCode: groupCode,
		DicKey:    dicKey,
	}
	rsp := common_rpc.R(common_rpc.UniversalServiceClientImpl().GetDicItemValue(stdContext.TODO(), req))
	if rsp.Success {
		dicItemInfo := &rpc_pbs.DicItemResp{}
		rsp.RpcData(dicItemInfo)
		resultValue := &common_models.DataDicItem{
			DicKey:       dicItemInfo.DicKey,
			DicLabel:     dicItemInfo.DicLabel,
			DicValueType: dic_value_type.DicValueType(dicItemInfo.DicValueType),
		}
		valueType := dic_value_type.DicValueType(dicItemInfo.DicValueType)
		if valueType == dic_value_type.String {
			resultValue.DicValue = dicItemInfo.DicValue
			return response.Success("获取成功", resultValue)
		} else if valueType == dic_value_type.Integer {
			value, e := strconv.Atoi(dicItemInfo.DicValue)
			resultValue.DicValue = value
			if e != nil {
				return response.Error(common_error.DicItemNotExist, nil)
			} else {
				return response.Success("获取成功", resultValue)
			}
		} else if valueType == dic_value_type.Decimal {
			value, e := decimal.NewFromString(dicItemInfo.DicValue)
			resultValue.DicValue = value.InexactFloat64()
			if e != nil {
				return response.Error(common_error.DicItemNotExist, nil)
			} else {
				return response.Success("获取成功", resultValue)
			}
		} else if valueType == dic_value_type.Bool {
			if strings.ToLower(dicItemInfo.DicValue) == "true" {
				resultValue.DicValue = true
			} else {
				resultValue.DicValue = false
			}
			return response.Success("获取成功", resultValue)
		} else if valueType == dic_value_type.JsonObject {
			value := make(map[string]any)
			json.ToObj(dicItemInfo.DicValue, &value)
			resultValue.DicValue = value
			return response.Success("获取成功", resultValue)
		} else if valueType == dic_value_type.JsonArray {
			value := make([]any, 0)
			json.ToObj(dicItemInfo.DicValue, &value)
			resultValue.DicValue = value
			return response.Success("获取成功", resultValue)
		} else {
			return response.Error(common_error.DicItemNotExist, nil)
		}
	} else {
		zlog.Info("获取字典项失败,msg:{}", rsp.Msg)
		return rsp
	}

}

// GetDicItemsValue 获取字典项集合值
func GetDicItemsValue(groupCode string) *response.Response {
	req := &rpc_pbs.DicItemReq{
		GroupCode: groupCode,
	}
	rsp := common_rpc.R(common_rpc.UniversalServiceClientImpl().GetDicItemByGroupCode(stdContext.TODO(), req))
	if rsp.Success {
		dicItemsResp := &rpc_pbs.DicItemsResp{}
		rsp.RpcData(dicItemsResp)
		values := make([]any, len(dicItemsResp.Items))
		for i, dicItemInfo := range dicItemsResp.Items {
			valueType := dic_value_type.DicValueType(dicItemInfo.DicValueType)
			if valueType == dic_value_type.String {
				values[i] = dicItemInfo.DicValue
			} else if valueType == dic_value_type.Integer {
				value, e := strconv.Atoi(dicItemInfo.DicValue)
				if e != nil {
					values[i] = value
				} else {
					return response.Error(common_error.DicItemNotExist, nil)
				}
			} else if valueType == dic_value_type.Decimal {
				value, e := decimal.NewFromString(dicItemInfo.DicValue)
				if e != nil {
					values[i] = value.InexactFloat64()
				} else {
					return response.Error(common_error.DicItemNotExist, nil)
				}
			} else if valueType == dic_value_type.Bool {
				if strings.ToLower(dicItemInfo.DicValue) == "true" {
					values[i] = true
				} else {
					values[i] = false
				}
			} else if valueType == dic_value_type.JsonObject {
				value := make(map[string]any)
				json.ToObj(dicItemInfo.DicValue, &value)
				values[i] = value
			} else if valueType == dic_value_type.JsonArray {
				value := make([]any, 0)
				json.ToObj(dicItemInfo.DicValue, &value)
				values[i] = value
			} else {
				return response.Error(common_error.DicItemNotExist, nil)
			}
		}
		return response.Success("获取成功", values)
	} else {
		zlog.Info("获取字典项失败,msg:{}", rsp.Msg)
		return rsp
	}
}

// GetDicItemsObj 获取字典项集合完整信息
func GetDicItemsObj(groupCode string) *response.Response {
	req := &rpc_pbs.DicItemReq{
		GroupCode: groupCode,
	}
	rsp := common_rpc.R(common_rpc.UniversalServiceClientImpl().GetDicItemByGroupCode(stdContext.TODO(), req))
	if rsp.Success {
		dicItemsResp := &rpc_pbs.DicItemsResp{}
		rsp.RpcData(dicItemsResp)
		values := make([]*common_models.DataDicItem, len(dicItemsResp.Items))
		for i, dicItemInfo := range dicItemsResp.Items {
			resultValue := &common_models.DataDicItem{
				DicKey:       dicItemInfo.DicKey,
				DicLabel:     dicItemInfo.DicLabel,
				DicValueType: dic_value_type.DicValueType(dicItemInfo.DicValueType),
			}
			valueType := dic_value_type.DicValueType(dicItemInfo.DicValueType)
			if valueType == dic_value_type.String {
				resultValue.DicValue = dicItemInfo.DicValue
			} else if valueType == dic_value_type.Integer {
				value, e := strconv.Atoi(dicItemInfo.DicValue)
				if e != nil {
					resultValue.DicValue = value
					values[i] = resultValue
				} else {
					return response.Error(common_error.DicItemNotExist, nil)
				}
			} else if valueType == dic_value_type.Decimal {
				value, e := decimal.NewFromString(dicItemInfo.DicValue)
				if e != nil {
					resultValue.DicValue = value.InexactFloat64()
					values[i] = resultValue
				} else {
					return response.Error(common_error.DicItemNotExist, nil)
				}
			} else if valueType == dic_value_type.Bool {
				if strings.ToLower(dicItemInfo.DicValue) == "true" {
					resultValue.DicValue = true
				} else {
					resultValue.DicValue = false
				}
				values[i] = resultValue
			} else if valueType == dic_value_type.JsonObject {
				value := make(map[string]any)
				json.ToObj(dicItemInfo.DicValue, &value)
				resultValue.DicValue = value
				values[i] = resultValue
			} else if valueType == dic_value_type.JsonArray {
				value := make([]any, 0)
				json.ToObj(dicItemInfo.DicValue, &value)
				resultValue.DicValue = value
				values[i] = resultValue
			} else {
				return response.Error(common_error.DicItemNotExist, nil)
			}
		}
		return response.Success("获取成功", values)
	} else {
		zlog.Info("获取字典项失败,msg:{}", rsp.Msg)
		return rsp
	}
}
